<!DOCTYPE html><html><head><title>Nginx的负载均衡与集群实战-4</title><meta charset='utf-8'><link href='https://dn-maxiang.qbox.me/res-min/themes/marxico.css' rel='stylesheet'><style></style></head><body><div id='preview-contents' class='note-content'>
                        
                    



<h4 id="aliasrewriteproxypass的区别">alias、rewrite、proxy_pass的区别？</h4>



<h2 id="第五堂课-nginx反向代理-负载均衡-及综合实例">第五堂课  Nginx反向代理 负载均衡 及综合实例</h2>



<h4 id="负载均衡">负载均衡</h4>



<pre class="prettyprint hljs-dark"><code class="hljs cpp">upstream blog.ha97.com {<br><span class="hljs-preprocessor">#upstream的负载均衡，weight是权重，可以根据机器配置定义权重。weigth参数表示权值，权值越高被分配到的几率越大。</span><br>server <span class="hljs-number">192.168</span><span class="hljs-number">.80</span><span class="hljs-number">.121</span>:<span class="hljs-number">80</span> weight=<span class="hljs-number">3</span>;<br>server <span class="hljs-number">192.168</span><span class="hljs-number">.80</span><span class="hljs-number">.122</span>:<span class="hljs-number">80</span> weight=<span class="hljs-number">2</span>;<br>server <span class="hljs-number">192.168</span><span class="hljs-number">.80</span><span class="hljs-number">.123</span>:<span class="hljs-number">80</span> weight=<span class="hljs-number">3</span>;<br>}<br></code></pre>

<blockquote>
  <p>blog.ha97.com  可以是别名，也可以是域名</p>
</blockquote>



<h4 id="一个关于负载均衡的配置实例">一个关于负载均衡的配置实例</h4>



<pre class="prettyprint hljs-dark"><code class="hljs cpp"><span class="hljs-preprocessor">#设定http服务器，利用它的反向代理功能提供负载均衡支持</span><br>http {<br><br>    <span class="hljs-preprocessor">#设定mime类型,类型由mime.type文件定义</span><br>    include             /etc/nginx/mime.types;<br>    default_type    application/octet-stream;<br><br>    <span class="hljs-preprocessor">#设定日志格式</span><br>    access_log        /var/<span class="hljs-built_in">log</span>/nginx/access.<span class="hljs-built_in">log</span>;<br><br>    <span class="hljs-preprocessor">#省略上文有的一些配置节点</span><br>    <span class="hljs-preprocessor">#。。。。。。。。。。</span><br><br>    <span class="hljs-preprocessor">#设定负载均衡的服务器列表</span><br>    upstream mysvr {<br>        <span class="hljs-preprocessor">#weigth参数表示权值，权值越高被分配到的几率越大</span><br>        server <span class="hljs-number">192.168</span><span class="hljs-number">.8</span><span class="hljs-number">.1</span>x:<span class="hljs-number">3128</span> weight=<span class="hljs-number">5</span>;<br>        <span class="hljs-preprocessor">#本机上的Squid开启<span class="hljs-number">3128</span>端口,不是必须要squid</span><br>        server <span class="hljs-number">192.168</span><span class="hljs-number">.8</span><span class="hljs-number">.2</span>x:<span class="hljs-number">80</span>    weight=<span class="hljs-number">1</span>;<br>        server <span class="hljs-number">192.168</span><span class="hljs-number">.8</span><span class="hljs-number">.3</span>x:<span class="hljs-number">80</span>    weight=<span class="hljs-number">6</span>;<br>    }<br><br>    upstream mysvr2 {<br>        <span class="hljs-preprocessor">#weigth参数表示权值，权值越高被分配到的几率越大</span><br>        server <span class="hljs-number">192.168</span><span class="hljs-number">.8</span>.x:<span class="hljs-number">80</span>    weight=<span class="hljs-number">1</span>;<br>        server <span class="hljs-number">192.168</span><span class="hljs-number">.8</span>.x:<span class="hljs-number">80</span>    weight=<span class="hljs-number">6</span>;<br>    }<br><br><br>    <span class="hljs-preprocessor">#第一个虚拟服务器</span><br>    server {<br>        <span class="hljs-preprocessor">#侦听<span class="hljs-number">192.168</span><span class="hljs-number">.8</span>.x的<span class="hljs-number">80</span>端口</span><br>        listen             <span class="hljs-number">80</span>;<br>        server_name    <span class="hljs-number">192.168</span><span class="hljs-number">.8</span>.x;<br><br>        <span class="hljs-preprocessor">#对aspx后缀的进行负载均衡请求</span><br>        location ~ .*.aspx$ {<br>            <span class="hljs-preprocessor">#定义服务器的默认网站根目录位置</span><br>            root     /root; <br>            <span class="hljs-preprocessor">#定义首页索引文件的名称</span><br>            index index.php index.html index.htm;<br><br>            <span class="hljs-preprocessor">#请求转向mysvr 定义的服务器列表</span><br>            proxy_pass    http:<span class="hljs-comment">//mysvr ;</span><br><br>            <span class="hljs-preprocessor">#以下是一些反向代理的配置可删除.</span><br><br>            proxy_redirect off;<br><br>            <span class="hljs-preprocessor">#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span><br>            proxy_set_header Host $host;<br>            proxy_set_header X-Real-IP $remote_addr;<br>            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br><br>            <span class="hljs-preprocessor">#允许客户端请求的最大单文件字节数</span><br>            client_max_body_size <span class="hljs-number">10</span>m; <br><br>            <span class="hljs-preprocessor">#缓冲区代理缓冲用户端请求的最大字节数，</span><br>            client_body_buffer_size <span class="hljs-number">128</span>k;<br><br>            <span class="hljs-preprocessor">#nginx跟后端服务器连接超时时间(代理连接超时)</span><br>            proxy_connect_timeout <span class="hljs-number">90</span>;<br><br>            <span class="hljs-preprocessor">#连接成功后，后端服务器响应时间(代理接收超时)</span><br>            proxy_read_timeout <span class="hljs-number">90</span>;<br><br>            <span class="hljs-preprocessor">#设置代理服务器（nginx）保存用户头信息的缓冲区大小</span><br>            proxy_buffer_size <span class="hljs-number">4</span>k;<br><br>            <span class="hljs-preprocessor">#proxy_buffers缓冲区，网页平均在<span class="hljs-number">32</span>k以下的话，这样设置</span><br>            proxy_buffers <span class="hljs-number">4</span> <span class="hljs-number">32</span>k;<br><br>            <span class="hljs-preprocessor">#高负荷下缓冲大小（proxy_buffers*<span class="hljs-number">2</span>）</span><br>            proxy_busy_buffers_size <span class="hljs-number">64</span>k; <br><br>            <span class="hljs-preprocessor">#设定缓存文件夹大小，大于这个值，将从upstream服务器传</span><br>            proxy_temp_file_write_size <span class="hljs-number">64</span>k;    <br><br>        }<br>    }<br>}<br></code></pre>

<blockquote>
  <p>upstream还可以为每个设备设置状态值，这些状态值的含义分别如下： <br>
  <strong>down</strong>  :  表示单前的server暂时不参与负载. <br>
  <strong>weight</strong>  :  默认为1.weight越大，负载的权重就越大。 <br>
  <strong>max_fails</strong> ：允许请求失败的次数默认为1.当超过最大次数时，返回proxy_next_upstream 模块定义的错误. <br>
  <strong>fail_timeout</strong> :  max_fails次失败后，暂停的时间。 <br>
  <strong>backup</strong> ：  其它所有的非backup机器down或者忙的时候，请求backup机器。所以这台机器压力会最轻。</p>
</blockquote>



<h4 id="反向代理">反向代理</h4>

<blockquote>
  <p>复习一下什么是反向代理, 回到第一堂课的笔记 <br>
  nginx作为web服务器一个重要的功能就是反向代理。 <br>
  当然你也可以使用nginx配置正向代理，本是介绍如何配置nginx的反向代理。 <br>
  nginx反向代理的指令不需要新增额外的模块，默认自带proxy_pass指令， <br>
  只需要修改配置文件就可以实现反向代理。</p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs nginx"><span class="hljs-comment">## Basic reverse proxy server ##</span><br><span class="hljs-comment">## Apache backend for www.quancha.cn ##</span><br><span class="hljs-title">upstream</span> apachephp  {<br>    <span class="hljs-title">server</span> ip:<span class="hljs-number">8080</span>; <span class="hljs-comment">#Apache</span><br>}<br><br><span class="hljs-comment">## Start www.quancha.cn ##</span><br><span class="hljs-title">server</span> {<br>    <span class="hljs-title">listen</span> <span class="hljs-number">80</span>;<br>    <span class="hljs-title">server_name</span>  www.quancha.cn;<br><br>    <span class="hljs-title">access_log</span>  logs/quancha.access.log  main;<br>    <span class="hljs-title">error_log</span>  logs/quancha.<span class="hljs-built_in">error</span>.log;<br>    <span class="hljs-title">root</span>   html;<br>    <span class="hljs-title">index</span>  index.html index.htm index.php;<br><br>    <span class="hljs-comment">## send request back to apache ##</span><br>    <span class="hljs-title">location</span> / {<br>        <span class="hljs-title">proxy_pass</span>  <span class="hljs-url">http://apachephp</span>;<br><br>        <span class="hljs-comment">#Proxy Settings</span><br>        <span class="hljs-title">proxy_redirect</span>     <span class="hljs-built_in">off</span>;<br>        <span class="hljs-title">proxy_set_header</span>   Host             <span class="hljs-variable">$host</span>;<br>        <span class="hljs-title">proxy_set_header</span>   X-Real-IP        <span class="hljs-variable">$remote_addr</span>;<br>        <span class="hljs-title">proxy_set_header</span>   X-Forwarded-For  <span class="hljs-variable">$proxy_add_x_forwarded_for</span>;<br>        <span class="hljs-title">proxy_next_upstream</span> <span class="hljs-built_in">error</span> timeout invalid_header http_500 http_502 http_503 http_504;<br>        <span class="hljs-title">proxy_max_temp_file_size</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-title">proxy_connect_timeout</span>      <span class="hljs-number">90</span>;<br>        <span class="hljs-title">proxy_send_timeout</span>         <span class="hljs-number">90</span>;<br>        <span class="hljs-title">proxy_read_timeout</span>         <span class="hljs-number">90</span>;<br>        <span class="hljs-title">proxy_buffer_size</span>          <span class="hljs-number">4k</span>;<br>        <span class="hljs-title">proxy_buffers</span>              <span class="hljs-number">4</span> <span class="hljs-number">32k</span>;<br>        <span class="hljs-title">proxy_busy_buffers_size</span>    <span class="hljs-number">64k</span>;<br>        <span class="hljs-title">proxy_temp_file_write_size</span> <span class="hljs-number">64k</span>;<br>   }<br>}<br><span class="hljs-comment">## End www.quancha.cn ##</span><br></code></pre>

<hr>



<h4 id="复习一下在linux环境中安装jdk">复习一下在Linux环境中安装JDK。</h4>

<blockquote>
  <p>1 .  copy 安装文件到/usr/local/java <br>
  2 .  解压:  </p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs stylus"> tar xvf jdk-<span class="hljs-number">7</span>u79-linux-x64<span class="hljs-class">.tar</span><span class="hljs-class">.gz</span><br></code></pre>

<blockquote>
  <p>3 . 删除安装包  </p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs stylus"> rm jdk-<span class="hljs-number">7</span>u79-linux-x64<span class="hljs-class">.tar</span><span class="hljs-class">.gz</span><br></code></pre>

<blockquote>
  <p>4 . 编辑环境变量</p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs crystal">vi    /etc/profile     <span class="hljs-comment">#编辑环境变量</span><br><br>export <span class="hljs-constant">JAVA_HOME</span>=<span class="hljs-regexp">/usr/local</span><span class="hljs-regexp">/java/jdk</span>1.<span class="hljs-number">7.0_45</span><br>export <span class="hljs-constant">JRE_HOME</span>=<span class="hljs-regexp">/usr/local</span><span class="hljs-regexp">/java/jdk</span>1.<span class="hljs-number">7.0_45</span>/jre<br>export <span class="hljs-constant">CLASSPATH</span>=.:<span class="hljs-variable">$JAVA_HOME</span>/<span class="hljs-class"><span class="hljs-keyword">lib</span>/<span class="hljs-title">dt</span>.<span class="hljs-title">jar</span>:$<span class="hljs-title">JAVA_HOME</span>/<span class="hljs-title">lib</span>/<span class="hljs-title">tools</span>.<span class="hljs-title">jar</span>:$<span class="hljs-title">JRE_HOME</span>/<span class="hljs-title">lib</span>:$<span class="hljs-title">CLASSPATH</span></span><br>export <span class="hljs-constant">PATH</span>=<span class="hljs-variable">$JAVA_HOME</span>/<span class="hljs-symbol">bin:</span><span class="hljs-variable">$PATH</span><br></code></pre>

<blockquote>
  <p>5 . 使用profile生效</p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs gradle"><span class="hljs-keyword">source</span> <span class="hljs-regexp">/etc/</span>profile<br></code></pre>

<blockquote>
  <p>6 . 检查安装是否成功</p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs applescript">java -<span class="hljs-property">version</span><br></code></pre>



<h4 id="tomcat的安装略">Tomcat的安装（略）</h4>



<h4 id="windows下tomcat的窗口名称修改">windows下Tomcat的窗口名称修改</h4>

<blockquote>
  <p>修改 catalina.bat</p>
</blockquote>

<p>找到下面的东东：  <br>
:doStart  <br>
shift  <br>
if not “%OS%” == “Windows_NT” goto noTitle  <br>
set _EXECJAVA=start “Tomcat_Title” %_RUNJAVA%  <br>
goto gotTitle </p>

<blockquote>
  <p>修改Tomcat_Title即可。</p>
</blockquote></div></body></html>